package upm.pb4agent.team8.Common;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Properties;

import upm.pb4agent.common.Constant;
import upm.pb4agent.team8.Common.PB4Utils.MsgTypes;

public class PB4Properties
{	
	private Properties _properties = null;
	public static HashMap<String,RowerAttribute > RowerAttributes;
	public static BoatAttributes BoatAttributes;
	
	public PB4Properties()
	{
		RowerAttributes = new HashMap<String, RowerAttribute>();
		BoatAttributes = new BoatAttributes();
		ReadConfigProperties();
		LoadGlobalProperties();
	}
	
	private void ReadConfigProperties()
	{
		if (_properties == null)
		{
			_properties = new Properties();
		}
        try
        {
        	_properties.load(new FileInputStream("C:/Users/Darwin/workspace/PB4/src/upm/pb4agent/config.properties"));   
        }
        catch (Exception e)
        {
        	PB4Utils.Console.WriteLine("Error trying to read Properties from the ConfigFiles",e);
        }
	}
	
	private void LoadGlobalProperties()
	{
    	Constant.REGISTRATION_PERIOD = ReadIntItemProperty("registration_period","2000");
    	Constant.START_RACE_PERIOD = ReadIntItemProperty("registration_period","2000");
    	Constant.CIRCUIT_LENGHT = ReadIntItemProperty("circuit_length","100");
    	
    	BoatAttributes.FrontOarXCoef = GetFloatKey("front_oar_x_coef");
    	BoatAttributes.FrontOarYCoef = GetFloatKey("front_oar_y_coef");
    	BoatAttributes.BackOarXCoef = GetFloatKey("back_oar_x_coef");
    	BoatAttributes.BackOarYCoef = GetFloatKey("back_oar_y_coef");
    	BoatAttributes.oar_x_coef = GetFloatKey("oar_x_coef");
    	BoatAttributes.oar_y_coef = GetFloatKey("oar_y_coef");
 	    	
    	//TODO: Improve this code.
    	LoadRowerAttributes("1");
    	LoadRowerAttributes("2");
    	LoadRowerAttributes("3");
    	LoadRowerAttributes("4");
	}
	
	private void LoadRowerAttributes(String ID)
	{
		RowerAttribute attr= new RowerAttribute();
		attr.Strength = GetFloatKey(Constant.ROWER_STRENGTH.replace("%", ID));
		attr.Resistance = GetFloatKey(Constant.ROWER_RESISTANCE.replace("%", ID));
		attr.Agility = GetFloatKey(Constant.ROWER_AGILITY.replace("%", ID));
		String rowerID = Constant.ROWER_NAME + Constant.TEAM_ID + ID;
		RowerAttributes.put(rowerID, attr);
	}
	
	public static float getImpulseValue( Constant.Impulse impulse)
	{
		switch (impulse) {
		case Weak:
			return PB4Utils.PropertyReader.GetFloatKey("impulse_weak");
		case Medium:
			return PB4Utils.PropertyReader.GetFloatKey("impulse_medium");
		case Strong:
			return PB4Utils.PropertyReader.GetFloatKey("impulse_strong");
		default:
			return 0;
		}
	}
	
	public int GetIntKey(String key)
	{
		return ReadIntItemProperty(key, "0");
	}
	
	public float GetFloatKey(String key)
	{
		return ReadFloatItemProperty(key, "0");
	}
	
	private int ReadIntItemProperty(String key, String defaultValue)
	{
		int iProp=0;
		String prop = _properties.getProperty(key,defaultValue);
		//PB4Utils.Console.WriteLine("Reading key \"" + key +"="+ prop +"\" + property from config ",MsgTypes.Low);
		try
		{
			return Integer.parseInt(prop);
		}
		catch (Exception e) 
		{
			PB4Utils.Console.WriteLine("Error trying to parse integer value",e);
		}
		
		return iProp;
	}
	
	private float ReadFloatItemProperty(String key, String defaultValue)
	{
		int iProp=0;
		String prop = _properties.getProperty(key,defaultValue);
		//PB4Utils.Console.WriteLine("Reading key \"" + key +"="+ prop +"\" + property from config ",MsgTypes.Low);
		try
		{
			return Float.parseFloat(prop);
		}
		catch (Exception e) 
		{
			PB4Utils.Console.WriteLine("Error trying to parse integer value",e);
		}
		
		return iProp;
	}
}
